<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Hunglv
 * @version         v.1.0
 * @created         11/6/2014
 * @Description :   Class controller Time Card
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Mod_timecard extends MYAD_Controller{
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Load library Model Mmod_timecard,Mmod_employee,Mmod_shiftinfo
 * @Input :         Null
 * @Output :        Null
 */ 
    protected $table_employee = 'm_employee';
    public function __construct() {
        parent::__construct();
        $this->load->model('Mmod_timecard');
        $this->load->model('mod_employee/Mmod_employee');
        $this->load->model('mod_tcmanipulation/Mmod_tcmanipulation');
        $this->load->model('mod_shiftinfo/Mmod_shiftinfo');
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Get all timecard by id, month, year of employee
 * @Input :         variable id,month,year 
 * @Output :       return array data timecard for view
 */    
    function index(){
        $id =  $this->input->post('txtEmployeeID');
        $month = $this->session->userdata('cmbMonth');
        $year = $this->session->userdata('cmbYear');
        $data['title'] = $this->lang->line('tc_title');
        $data['show'] = "index";
        $data['js'] = array('jquery.numeric.js');
        $temp = array();
        if(is_numeric($id)&&is_numeric($month)&&is_numeric($year)&&($this->Mmod_timecard->checkExistEmployee($id))){
            if($this->input->post('btnSubmit')){ 
                $count = $this->input->post('count');
                for($i =1; $i<$count;$i++){
                    $idCard = $_POST['icard'][$i];
                    $data1 = array(
                        'TrimmedArrivalTime'    => $_POST['ArrivalTime1'][$i].":".$_POST['ArrivalTime2'][$i].":00",
                        'TrimmedLeaveTime'      => $_POST['LeaveTime1'][$i].":".$_POST['LeaveTime2'][$i].":00",
                        'ResTime1'              => $this->getRestTimeHour($_POST['ResTime11'][$i],$_POST['ResTime12'][$i]),
                        'ResTime2'              => $this->getRestTimeHour($_POST['ResTime21'][$i],$_POST['ResTime22'][$i]),
                        'ResTime3'              => $this->getRestTimeHour($_POST['ResTime31'][$i],$_POST['ResTime32'][$i]),
                        'WeekdayNormalTime'     => $this->getTimeWorkingHour($_POST['WeekdayNormalTime10'][$i]),
                        'WeekdayOvertime'       => $this->getTimeWorkingHour($_POST['WeekdayOvertime10'][$i]),
                        'WeekdayLateOvertime'   => $this->getTimeWorkingHour($_POST['WeekdayLateOvertime10'][$i]),
                        'WeekendOvertime'       => $this->getTimeWorkingHour($_POST['WeekendOvertime10'][$i]),
                        'WeekendLateOvertime'   => $this->getTimeWorkingHour($_POST['WeekendLateOvertime10'][$i]),
                        'HolidayOvertime'       => $this->getTimeWorkingHour($_POST['HolidayOvertime10'][$i]),
                        'HolidayLateOvertime'   => $this->getTimeWorkingHour($_POST['HolidayLateOvertime10'][$i]),
                        'ActualWorkHours'       => $this->getTimeWorkingHour($_POST['ActualWorkHours10'][$i]),
                    );
                    $result = $this->Mmod_timecard->updateDataName($data1,$idCard, "TimeCardId");
                }
                if($result == TRUE){
                   redirect('/listtimecard');
                }                
            }
            else{
                $condition = array(
                    'where' => array(
                        'EmployeeId' => $id,
                    ),
                    'disable_del' => TRUE,
                    'first' => true,
                );                
                $employeeInfo=$this->Mmod_timecard->selectDB($this->table_employee,$condition);
                if($employeeInfo){
                    $data['employee'] = $employeeInfo->EmployeeName;
                    $data['data'] = $this->Mmod_timecard->listTimeCardByEmployeeMonthYear($id,$month,$year);
                    $temp['shiftInfoConfig'] = $this->Mmod_timecard->getShiftInfo();
                }
                else{
                    $data['message'] = $this->lang->line('tc_errormes');               
                }                
            }           
        }
        else{
            redirect('/listtimecard');       
        }
        $data1 = array_merge($data,$temp);
        $this->load->view('layout', $data1); 
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:   Caculation time working holiday one day of employee
 * @Input :        null
 * @Output :       Return json for ajax view
 */       
    function caculationHolidayWorking(){
        $shiftInfo = $this->Mmod_tcmanipulation->listShiftInfo();
        $ArrivalTime1 = $this->reChangeTime1($_POST['ArrivalTime1'],$_POST['ORGArrivalTime1']);
        $ArrivalTime2 = $this->reChangeTime2($_POST['ArrivalTime2'],$_POST['ORGArrivalTime2']);
        $LeaveTime1 = $this->reChangeTime1($_POST['LeaveTime1'],$_POST['ORGLeaveTime1']);
        $LeaveTime2 = $this->reChangeTime2($_POST['LeaveTime2'],$_POST['ORGLeaveTime2']);
        $Arrivaltime_temp = $ArrivalTime1.":".$ArrivalTime2.":00";
        $Leavetime_temp = $LeaveTime1.":".$LeaveTime2.":00";
        if(strtotime($Arrivaltime_temp)>=strtotime($Leavetime_temp)){
            $ArrivalTime1 = $_POST['ORGArrivalTime1'];
            $LeaveTime1 = $_POST['ORGLeaveTime1'];
        }
        $Arrivaltime = $ArrivalTime1.":".$ArrivalTime2.":00";
        $Leavetime = $LeaveTime1.":".$LeaveTime2.":00"; 
        $Date = $_POST['DateDay'];
        $employeeID = $_POST['employeeID'];
        $Arrivaltime = $this->reCaculationArrivaltime($Arrivaltime,$shiftInfo['StartTime']);
        $data = $this->Mmod_timecard->timeCardWorkingHoliDay($Arrivaltime,$Leavetime,$Date,$employeeID,$shiftInfo);
        echo json_encode($data);
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:   Caculation time working normal one day of employee
 * @Input :        null
 * @Output :       Return json for ajax view
 */       
    function caculationNormalWorking(){
        $shiftInfo = $this->Mmod_tcmanipulation->listShiftInfo();
        $ArrivalTime1 = $this->reChangeTime1($_POST['ArrivalTime1'],$_POST['ORGArrivalTime1']);
        $ArrivalTime2 = $this->reChangeTime2($_POST['ArrivalTime2'],$_POST['ORGArrivalTime2']);
        $LeaveTime1 = $this->reChangeTime1($_POST['LeaveTime1'],$_POST['ORGLeaveTime1']);
        $LeaveTime2 = $this->reChangeTime2($_POST['LeaveTime2'],$_POST['ORGLeaveTime2']);       
        $Arrivaltime_temp = $ArrivalTime1.":".$ArrivalTime2.":00";
        $Leavetime_temp = $LeaveTime1.":".$LeaveTime2.":00";
        if(strtotime($Arrivaltime_temp)>=strtotime($Leavetime_temp)){
            $ArrivalTime1 = $_POST['ORGArrivalTime1'];
            $LeaveTime1 = $_POST['ORGLeaveTime1'];
        }
        $Arrivaltime = $ArrivalTime1.":".$ArrivalTime2.":00";
        $Leavetime = $LeaveTime1.":".$LeaveTime2.":00";        
        $Date = $_POST['DateDay'];
        $employeeID = $_POST['employeeID'];
        $Arrivaltime = $this->reCaculationArrivaltime($Arrivaltime,$shiftInfo['StartTime']);
        $data = $this->Mmod_timecard->timeCardWorkingNormalDay($Arrivaltime,$Leavetime,$Date,$employeeID,$shiftInfo);
        $data['WorkingHours'] = 0;
        echo json_encode($data);
    }  
/*
 * @author :        HungLV
 * @created:        
 * @Description:   Caculation time working normal one day of employee
 * @Input :        null
 * @Output :       Return json for ajax view
 */       
    function caculationWeekendWorking(){
        $shiftInfo = $this->Mmod_tcmanipulation->listShiftInfo();
        $ArrivalTime1 = $this->reChangeTime1($_POST['ArrivalTime1'],$_POST['ORGArrivalTime1']);
        $ArrivalTime2 = $this->reChangeTime2($_POST['ArrivalTime2'],$_POST['ORGArrivalTime2']);
        $LeaveTime1 = $this->reChangeTime1($_POST['LeaveTime1'],$_POST['ORGLeaveTime1']);
        $LeaveTime2 = $this->reChangeTime2($_POST['LeaveTime2'],$_POST['ORGLeaveTime2']);
        $Arrivaltime_temp = $ArrivalTime1.":".$ArrivalTime2.":00";
        $Leavetime_temp = $LeaveTime1.":".$LeaveTime2.":00";
        if(strtotime($Arrivaltime_temp)>=strtotime($Leavetime_temp)){
            $ArrivalTime1 = $_POST['ORGArrivalTime1'];
            $LeaveTime1 = $_POST['ORGLeaveTime1'];
        }
        $Arrivaltime = $ArrivalTime1.":".$ArrivalTime2.":00";
        $Leavetime = $LeaveTime1.":".$LeaveTime2.":00"; 
        $Date = $_POST['DateDay'];
        $employeeID = $_POST['employeeID'];
        $Arrivaltime = $this->reCaculationArrivaltime($Arrivaltime,$shiftInfo['HalfDayStartTime']);
        $data = $this->Mmod_timecard->timeCardWorkingWeekendDay($Arrivaltime,$Leavetime,$Date,$employeeID,$shiftInfo);
        $data['WorkingHours'] = 0;
        echo json_encode($data);
    }     
    function getRestTimeHour($hour,$mute){
        $Mtime = $hour*60;
        $Mtime = $Mtime + $mute;
        $hourTime = round($Mtime/60,2);
        return $hourTime;
    }
    function getTimeWorkingHour($inputTime){
        $cInputTime = explode(":", $inputTime);
        $Mtime = $cInputTime[0]*60;
        $Mtime = $Mtime + $cInputTime[1];
        $hourTime = round($Mtime/60,2);
        return $hourTime;
    }
    function reCaculationArrivaltime($Arrivaltime,$StartTime){
        if(strtotime($Arrivaltime)< strtotime($StartTime)){
            return $StartTime;
        }
        else{
            return $Arrivaltime;
        }
    }
    function caculationRestime(){
        $Arrivaltime = $_POST['Arrivaltime'];
        $Leavetime = $_POST['Leavetime'];
        $Date = $_POST['DateDay'];
        $employeeID = $_POST['employeeID'];
        $IDCARD = $_POST['IDCARD'];
        $dateType = $_POST['dateType'];
        $shiftInfo = $this->Mmod_tcmanipulation->listShiftInfo();
        switch ($dateType) {
            case 1:
                $Arrivaltime = $this->reCaculationArrivaltime($Arrivaltime,$shiftInfo['StartTime']);
                $data = $this->Mmod_timecard->timeCardWorkingNormalDay($Arrivaltime,$Leavetime,$Date,$employeeID,$shiftInfo);
                echo json_encode($data);
                break;
            case 2:
                $Arrivaltime = $this->reCaculationArrivaltime($Arrivaltime,$shiftInfo['HalfDayStartTime']);
                $data = $this->Mmod_timecard->timeCardWorkingWeekendDay($Arrivaltime,$Leavetime,$Date,$employeeID,$shiftInfo);
                echo json_encode($data);                
                break;
            case 3:
                $Arrivaltime = $this->reCaculationArrivaltime($Arrivaltime,$shiftInfo['StartTime']);
                $data = $this->Mmod_timecard->timeCardWorkingHoliDay($Arrivaltime,$Leavetime,$Date,$employeeID,$shiftInfo);
                echo json_encode($data);
                break;            
        }
    }
    /*
     * @author :        HungLV
     * @created:        
     * @Description:   re change ArrivalTime time1 input
     * @Input :        
     * @Output :       Return ajax for view
     */
    function reChangeInputArrivalTimeTime1(){
        $Time11 = 0;
        $ORGTime = $_POST['ORGArrivalTime1'];
        $LeaveTime1 = $_POST['LeaveTime1'];
        $ArrivalTime2 = $_POST['ArrivalTime2'];
        $LeaveTime2 = $_POST['LeaveTime2'];
        $ArrivalTime1 = mb_convert_kana($_POST['ArrivalTime1'],'a');
        $ArrivalTime = $ArrivalTime1.":".$ArrivalTime2.":00";
        $LeaveTime = $LeaveTime1.":".$LeaveTime2.":00";
        if(strtotime($ArrivalTime)>=strtotime($LeaveTime)){
            $Time11 = $ORGTime;
        }
        else{
            $Time11 = $ArrivalTime1;
        }
        if(is_numeric($Time11)){
            if((int)$Time11>0){
                echo $Time11;
            }
            else{
                echo $ORGTime;
            }            
        }
        else{
            echo $ORGTime;
        }
    }
    /*
     * @author :        HungLV
     * @created:        
     * @Description:   re change ArrivalTime time1 input
     * @Input :        
     * @Output :       Return ajax for view
     */
    function reChangeInputLeaveTime1(){
        $Time11 = 0;
        $ORGTime = $_POST['ORGLeaveTime1'];
        $ArrivalTime1 = $_POST['ArrivalTime1'];
        $ArrivalTime2 = $_POST['ArrivalTime2'];
        $LeaveTime2 = $_POST['LeaveTime2'];        
        $EmployeeId = $_POST['EmployeeId'];
        $DateDay = $_POST['DateDay'];
        $LeaveTime1 = mb_convert_kana($_POST['LeaveTime1'],'a');
        $ArrivalTime = $ArrivalTime1.":".$ArrivalTime2.":00";
        $LeaveTime = $LeaveTime1.":".$LeaveTime2.":00";
        if(strtotime($LeaveTime)<=strtotime($ArrivalTime)){
            $Time11 = $ORGTime;
        }
        else{
            if($this->Mmod_tcmanipulation->isCheckOvertime($EmployeeId,$DateDay) && $LeaveTime1<24){
               $Time11 = $LeaveTime1;
            }
            else{
               $Time11 = $ORGTime;
            }
        }
        if(is_numeric ($Time11)){
            if((int)$Time11>0){
                echo $Time11;
            }
            else{
                echo $ORGTime;
            }            
        }
        else{
            echo $ORGTime;
        }
    }    
    /*
     * @author :        HungLV
     * @created:        
     * @Description:   re Change Time2 
     * @Input :        
     * @Output :       Return Time2
     */
    function reChangeInputTime2(){
        $Time2 = $_POST['Time2'];
        $ORGTime = $_POST['ORGTime'];        
        $shiftInfo = $this->Mmod_tcmanipulation->listShiftInfo();
        $Time21 = mb_convert_kana($Time2,'a');
        if(is_numeric ($Time21)){
            if((int)$Time21>=$shiftInfo['RoundedTimeBlock']){
                echo $shiftInfo['RoundedTimeBlock'];
            }
            else{
                echo '00';
            }            
        }
        else{
            echo $ORGTime;
        }
    }     
    /*
     * @author :        HungLV
     * @created:        
     * @Description:   re Change Time1 
     * @Input :        
     * @Output :       Return Time1 
     */
    function reChangeTime1($Time1,$ORGTime){
        $Time11 = mb_convert_kana($Time1,'a');
        if(is_numeric ($Time11)){
            if((int)$Time11>0){
                return $Time11;
            }
            else{
                return $ORGTime;
            }            
        }
        else{
            return $ORGTime;
        }
    }    
    /*
     * @author :        HungLV
     * @created:        
     * @Description:   re Change Time2 
     * @Input :        
     * @Output :       Return Time2
     */
    function reChangeTime2($Time2,$ORGTime){
        $shiftInfo = $this->Mmod_tcmanipulation->listShiftInfo();
        $Time21 = mb_convert_kana($Time2,'a');
        if(is_numeric ($Time21)){
            if((int)$Time21>=$shiftInfo['RoundedTimeBlock']){
                return $shiftInfo['RoundedTimeBlock'];
            }
            else{
                return 0;
            }            
        }
        else{
            return $ORGTime;
        }
    }
    /*
     * @author :       HungLV
     * @created:        
     * @Description:   change working data when click select working
     * @Input :        (Array) Post
     * @Output :       Return json
     */
    function changeWorking(){
        // get data
        $date = $_POST['Date'];
        // get EmployeeId
        $employeeID = $_POST['EmployeeId'];
        // get ShiftType
        $shiftType = $_POST['ShiftType'];
        // get ArrivalTime1
        $arrivalTime1 = $_POST['ArrivalTime1'];
        // get ArrivalTime2
        $arrivalTime2 = $_POST['ArrivalTime2'];
        // get LeaveTime1
        $leaveTime1 = $_POST['LeaveTime1'];
        // get LeaveTime2
        $leaveTime2 = $_POST['LeaveTime2'];
        
        echo $arrivalTime1."----".$leaveTime1;
    }
}
?>
